﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Common;
using TPCN.Commons;

namespace TPCN
{
    public partial class Logon : System.Web.UI.Page
    {
        string returnurl = "~/Default.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["returnurl"] != null)
            {
                returnurl = Request.QueryString["returnurl"].ToString();
            }
        }

        protected void LoginButton_Click(object sender, ImageClickEventArgs e)
        {
            string username = Username.Text.ToLower().Trim();
            string password = Password.Text.Trim();
            if (AutherizeSuccess(username, password))
            {
                Response.Redirect(returnurl);
            }
            else
            {
                message.Text = "Email hoặc mật khẩu không chính xác!";
                errorpanel.Visible = true;
            }
        }

        protected bool AutherizeSuccess(string email, string password)
        {
            KhachHangObj obj = KhachHangDAL.GetByEmail(email);
            if (obj != null)
            {
                if (obj.Password == Security.EncryptPassword(password))
                {
                    HttpContext.Current.Session["user"] = obj.KhachID;
                    return true;
                }
            }
            return false;
        }
    }
}